package cn.wikiflyer.beikang.act.user;

import java.lang.reflect.Type;
import java.util.ArrayList;

import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
import cn.wikiflyer.beikang.BaseActivity;
import cn.wikiflyer.beikang.R;
import cn.wikiflyer.beikang.act.user.adapter.AddressAdapter;
import cn.wikiflyer.beikang.act.user.adapter.AddressAdapter.OnCheckListener;
import cn.wikiflyer.beikang.act.user.bean.BKAddressResultBean;
import cn.wikiflyer.beikang.bean.base.BKBaseBean;
import cn.wk.libs4a.net.WKAsyncTaskPro;
import cn.wk.libs4a.view.WKHeader;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
 * 地址管理
 * 
 * @author qunlee_mr
 * 
 */
public class UserAddressListAct extends BaseActivity {

	@ViewInject(id = R.id.header)
	private WKHeader header;
	private ListView mListView;
	private AddressAdapter mAddressAdapter;

	@Override
	public void loadXml() {
		setContentView(R.layout.bk_address_list_layout);// common_layout布局作为列表通用布局
	}

	@Override
	public void init() {
		header.setTitle("地址管理");
		header.setLeftImg("新增", R.drawable.user_addadd_img, false, 12);
		header.setClickListener(null, null, new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// WKApplication.showMsg("添加");
				Intent intent = new Intent(ctx, UserAddressAAndUAct.class);
				startActivity(intent);
			}
		});
		mListView = (ListView) findViewById(R.id.listView1);
		mAddressAdapter = new AddressAdapter(ctx);
		mListView.setAdapter(mAddressAdapter);
		mAddressAdapter.setOnCheckListener(new OnCheckListener() {
			public void checkBox(String addid) {
				new DefaultAddressTask(ctx, addid);
			}

			public void delete(String addid) {
				new DeleteAddressTask(ctx, addid);
			}
		});
	}

	@Override
	public void setData() {
	}

	@Override
	protected void onResume() {
		super.onResume();
		new GetAddressListTask(ctx);
	}

	/**
	 * 收货地址列表
	 */
	class GetAddressListTask extends WKAsyncTaskPro {
		public GetAddressListTask(Context context) {
			super(context, true);
		}

		@Override
		protected String doInBackground(boolean... isLocal) {
			return app().net.getAddressList(isLocal);
		}

		@Override
		protected void doOnPostExecute(String result, boolean isLocal) {
			Type cvbType = new TypeToken<BKBaseBean<ArrayList<BKAddressResultBean>>>() {
			}.getType();
			Gson gson = new Gson();
			BKBaseBean<ArrayList<BKAddressResultBean>> base = gson.fromJson(
					result, cvbType);
			mAddressAdapter.setData(base.data);
		}
	}

	/**
	 * 设置默认地址
	 */
	class DefaultAddressTask extends WKAsyncTaskPro {
		private String addid;

		public DefaultAddressTask(Context context, String addid) {
			super(context);
			this.addid = addid;
		}

		@Override
		protected String doInBackground(boolean... params) {
			return app().net.defaultAddress("" + addid);
		}

		@Override
		protected void doOnPostExecute(String result, boolean isLocal) {
			Type cvbType = new TypeToken<BKBaseBean>() {
			}.getType();
			Gson gson = new Gson();
			BKBaseBean base = gson.fromJson(result, cvbType);
			if (base.status == 1) {
				new GetAddressListTask(ctx);
			} else {
				Toast.makeText(ctx, "更新失败", 0).show();
			}
		}
	}

	/**
	 * 删除收货地址
	 */
	class DeleteAddressTask extends WKAsyncTaskPro {
		private String addid;

		public DeleteAddressTask(Context context, String addid) {
			super(context);
			this.addid = addid;
		}

		@Override
		protected String doInBackground(boolean... params) {
			return app().net.deleteAddress(addid);
		}

		@Override
		protected void doOnPostExecute(String result, boolean isLocal) {
			Type cvbType = new TypeToken<BKBaseBean>() {
			}.getType();
			Gson gson = new Gson();
			BKBaseBean base = gson.fromJson(result, cvbType);
			if (base.status == 1) {
				new GetAddressListTask(ctx);
			} else {
				Toast.makeText(ctx, "删除失败", 0).show();
			}
		}
	}

}
